package com.lanxin.xxform.services.impls;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanxin.xxform.aspects.exceptions.ServiceException;
import com.lanxin.xxform.beans.Subject;
import com.lanxin.xxform.mappers.SubjectMapper;
import com.lanxin.xxform.services.SubjectService;
import com.lanxin.xxform.tools.JetCriteria;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public int save(Subject subject) {
        if (subject.getId() == 0) {
            return subjectMapper.insert(subject);
        } else {
            Subject s = get(subject.getId());
            s.setSubtitle(subject.getSubtitle());
            s.setOptions(subject.getOptions());
            s.setType(subject.getType());
            s.setChecked(subject.getChecked());
            return subjectMapper.update(subject);
        }
    }

    @Override
    public Subject get(long id) {
        Subject subject = subjectMapper.find(id);
        if (subject == null) {
            throw new ServiceException("题目信息不存在");
        }
        return subject;
    }

    @Override
    public int getCount(long pid) {
        JetCriteria criteria = new JetCriteria();
        criteria.add("pid", pid);
        return subjectMapper.count(criteria);
    }

    @Override
    public List<Subject> getList(long pid) {
        JetCriteria criteria = new JetCriteria();
        criteria.add("pid", pid);
        criteria.orderBy("id", JetCriteria.ORDER_ASC);
        return subjectMapper.findAll(criteria);
    }

    @Override
    public int delete(long id, long uid) {
        return subjectMapper.delete(id);
    }

    @Override
    public int deleteByPaper(long pid, long uid) {
        return 0;
    }
    
}
